import unittest
import requests
import logging
import utils
from api.login import LoginApi
from utils import use_sql
from parameterized import parameterized


class TestRegister(unittest.TestCase):

    def setUp(self) -> None:
        self.session = requests.session()
        self.login_api = LoginApi(self.session)

        # 清理数据库垃圾数据
        sql1 = "delete from mb_member where name = '13203003591'"
        sql2 = "delete from mb_member_info where member_name = '13203003591'"
        sql3 = "delete from mb_member_register_log where member_name = '13203003591'"
        utils.DBUtil.update_one(sql1, sql2, sql3)

    def tearDown(self) -> None:
        self.session.close()

    # 注册成功（仅填入必填参数）
    @parameterized.expand(utils.read_file("register_data"))
    def test01_register_success_only_params(self, phone, password, img_verify_code, phone_code, dy_server, invite_phone,
                                            status_code, status, description_mes, description_register):
        # 测试数据
        # phone = "13203003591"
        # password = "hyc123456"
        # img_verify_code = "8888"
        # phone_code = "666666"
        # dy_server = "on"

        # 调用接口
        res_img = self.login_api.get_img_verify_code(1)
        res_info = self.login_api.get_info_verify_code(phone, img_verify_code)
        res_register = self.login_api.register(phone, password, img_verify_code, phone_code, dy_server, invite_phone)

        # 断言
        self.assertEqual(status_code, res_img.status_code)
        utils.common_assert(self, res_info, status_code, status, description_mes)
        utils.common_assert(self, res_register, status_code, status, description_register)

        # 输出日志
        logging.info(f"注册结果为：{res_register.json()}")

    # 注册成功（填入所有参数）
    # def test02_register_success_all_params(self):
    #     # 测试数据
    #     phone = "13203003591"
    #     password = "hyc123456"
    #     img_verify_code = "8888"
    #     phone_code = "666666"
    #     dy_server = "on"
    #     invite_phone = "13203003592"
    #
    #     # 调用接口
    #     res_img = self.login_api.get_img_verify_code(1)
    #     res_info = self.login_api.get_info_verify_code(phone, img_verify_code)
    #     res_register = self.login_api.register(phone, password, img_verify_code, phone_code, dy_server, invite_phone)
    #
    #     # 断言
    #     self.assertEqual(200, res_img.status_code)
    #     utils.common_assert(self, res_info, 200, 200, "短信发送成功")
    #     utils.common_assert(self, res_register, 200, 200, "注册成功")
    #
    #     # 输出日志
    #     logging.info(f"注册结果为：{res_register.json()}")
    #
    # # 注册失败（图片验证码错误）
    # def test03_register_fail_img_false(self):
    #     # 测试数据
    #     phone = "13203003591"
    #     password = "hyc123456"
    #     img_verify_code = "8881"
    #     img_verify_code_info = "8888"
    #     phone_code = "666666"
    #     dy_server = "on"
    #
    #     # 调用接口
    #     res_img = self.login_api.get_img_verify_code(1)
    #     res_info = self.login_api.get_info_verify_code(phone, img_verify_code_info)
    #     res_register = self.login_api.register(phone, password, img_verify_code, phone_code, dy_server)
    #
    #     # 断言
    #     self.assertEqual(200, res_img.status_code)
    #     utils.common_assert(self, res_info, 200, 200, "短信发送成功")
    #     utils.common_assert(self, res_register, 200, 100, "验证码错误")
    #
    #     # 输出日志
    #     logging.info(f"注册结果为：{res_register.json()}")
    #
    # # 注册失败（短信证码错误）
    # def test04_register_fail_info_false(self):
    #     # 测试数据
    #     phone = "13203003591"
    #     password = "hyc123456"
    #     img_verify_code_info = "8888"
    #     phone_code = "666667"
    #     dy_server = "on"
    #
    #     # 调用接口
    #     res_img = self.login_api.get_img_verify_code(1)
    #     res_info = self.login_api.get_info_verify_code(phone, img_verify_code_info)
    #     res_register = self.login_api.register(phone, password, img_verify_code_info, phone_code, dy_server)
    #
    #     # 断言
    #     self.assertEqual(200, res_img.status_code)
    #     utils.common_assert(self, res_info, 200, 200, "短信发送成功")
    #     utils.common_assert(self, res_register, 200, 100, "验证码错误")
    #
    #     # 输出日志
    #     logging.info(f"注册结果为：{res_register.json()}")
    #
    # # 注册失败（图片验证码为空）
    # def test05_register_fail_img_null(self):
    #     # 测试数据
    #     phone = "13203003591"
    #     password = "hyc123456"
    #     img_verify_code = "8888"
    #     img_verify_code_info = None
    #     phone_code = "666666"
    #     dy_server = "on"
    #
    #     # 调用接口
    #     res_img = self.login_api.get_img_verify_code(1)
    #     res_info = self.login_api.get_info_verify_code(phone, img_verify_code)
    #     res_register = self.login_api.register(phone, password, img_verify_code_info, phone_code, dy_server)
    #
    #     # 断言
    #     self.assertEqual(200, res_img.status_code)
    #     utils.common_assert(self, res_info, 200, 200, "短信发送成功")
    #     utils.common_assert(self, res_register, 200, 100, "验证码不能为空")
    #
    #     # 输出日志
    #     logging.info(f"注册结果为：{res_register.json()}")
    #
    # # 注册失败（短信证码为空）
    # def test06_register_fail_info_null(self):
    #     # 测试数据
    #     phone = "13203003591"
    #     password = "hyc123456"
    #     img_verify_code_info = "8888"
    #     phone_code = None
    #     dy_server = "on"
    #
    #     # 调用接口
    #     res_img = self.login_api.get_img_verify_code(1)
    #     res_info = self.login_api.get_info_verify_code(phone, img_verify_code_info)
    #     res_register = self.login_api.register(phone, password, img_verify_code_info, phone_code, dy_server)
    #
    #     # 断言
    #     self.assertEqual(200, res_img.status_code)
    #     utils.common_assert(self, res_info, 200, 200, "短信发送成功")
    #     utils.common_assert(self, res_register, 200, 100, "短信验证码不能为空")
    #
    #     # 输出日志
    #     logging.info(f"注册结果为：{res_register.json()}")
    #
    # # 注册失败（手机号已存在）
    # def test07_register_success_phone_repeat(self):
    #     # 测试数据
    #     phone = "13203003592"
    #     password = "hyc123456"
    #     img_verify_code = "8888"
    #     phone_code = "666666"
    #     dy_server = "on"
    #
    #     # 调用接口
    #     res_img = self.login_api.get_img_verify_code(1)
    #     res_info = self.login_api.get_info_verify_code(phone, img_verify_code)
    #     res_register = self.login_api.register(phone, password, img_verify_code, phone_code, dy_server)
    #
    #     # 断言
    #     self.assertEqual(200, res_img.status_code)
    #     utils.common_assert(self, res_info, 200, 200, "短信发送成功")
    #     utils.common_assert(self, res_register, 200, 100, "手机已存在")
    #
    #     # 输出日志
    #     logging.info(f"注册结果为：{res_register.json()}")
    #
    # # 注册失败（密码不能为空）---BUG
    # def test08_register_success_password_null(self):
    #     # 测试数据
    #     phone = "13203003591"
    #     password = None
    #     img_verify_code = "8888"
    #     phone_code = "666666"
    #     dy_server = "on"
    #
    #     # 调用接口
    #     res_img = self.login_api.get_img_verify_code(1)
    #     res_info = self.login_api.get_info_verify_code(phone, img_verify_code)
    #     res_register = self.login_api.register(phone, password, img_verify_code, phone_code, dy_server)
    #
    #     # 断言
    #     self.assertEqual(200, res_img.status_code)
    #     utils.common_assert(self, res_info, 200, 200, "短信发送成功")
    #     utils.common_assert(self, res_register, 200, 100, "密码不能为空")
    #
    #     # 输出日志
    #     logging.info(f"注册结果为：{res_register.json()}")
    #
    # # 注册失败（不同意协议）---BUG
    # def test09_register_success_password_null(self):
    #     # 测试数据
    #     phone = "13203003591"
    #     password = "hyc123456"
    #     img_verify_code = "8888"
    #     phone_code = "666666"
    #     dy_server = "off"
    #
    #     # 调用接口
    #     res_img = self.login_api.get_img_verify_code(1)
    #     res_info = self.login_api.get_info_verify_code(phone, img_verify_code)
    #     res_register = self.login_api.register(phone, password, img_verify_code, phone_code, dy_server)
    #
    #     # 断言
    #     self.assertEqual(200, res_img.status_code)
    #     utils.common_assert(self, res_info, 200, 200, "短信发送成功")
    #     utils.common_assert(self, res_register, 200, 100, "请同意我们的条款")
    #
    #     # 输出日志
    #     logging.info(f"注册结果为：{res_register.json()}")
